package de.mfits.fireflyplayer;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.SimpleCursorAdapter;
import de.mfits.fireflyplayer.storage.DbHelper;

public abstract class BrowseActivity extends ListActivity {
  private final DbHelper dbHelper = new DbHelper(this);

  private final String table;
  private final String[] columns;
  private final String selection;
  private String[] selectionArgs;
  private final String groupBy;
  private final String orderBy;
  private final String having;

  private final int layout;
  private final String[] from;
  private final int[] to;

  protected Cursor cursor;

  public BrowseActivity(String table, String[] columns, String selection, String groupBy,
      String having, String orderBy, int layout, String[] from, int[] to) {
    this.table = table;
    this.columns = columns;
    this.selection = selection;
    this.groupBy = groupBy;
    this.having = having;
    this.orderBy = orderBy;
    this.from = from;
    this.to = to;
    this.layout = layout;
  }

  protected void setSelectionArgs(String[] selectionArgs) {
    this.selectionArgs = selectionArgs;
  }

  @Override
  protected void onResume() {
    super.onResume();

    SQLiteDatabase db = dbHelper.getReadableDatabase();

    cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    startManagingCursor(cursor);

    setListAdapter(new SimpleCursorAdapter(this, layout, cursor, from, to));

    db.close();
  }
}
